package com.jcfk.eam.controller.mould;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.domain.dto.mould.EamMouldPackageInfoDTO;
import com.jcfk.eam.domain.po.mould.EamMouldPackageInfo;
import com.jcfk.eam.domain.query.mould.EamMouldPackageInfoQuery;
import com.jcfk.eam.service.mould.EamMouldPackageInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 模具模板框数据
 *
 * @author ly
 * @email 522891237@gqq.com
 * @date 2025-08-12
 */
@Api(tags = {"模具模板框数据管理"})
@RestController
@RequestMapping("eammouldpackageinfo")
public class EamMouldPackageInfoController {
    @Autowired
    private EamMouldPackageInfoService eamMouldPackageInfoService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamMouldPackageInfoQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询模具模板框数据")
    @PostMapping("/queryByPage")
    public RetPage<EamMouldPackageInfoDTO> queryByPage(@RequestBody ReqPageParam<EamMouldPackageInfoQuery> requestParam) {
        DataPage<EamMouldPackageInfoDTO> page = new DataPage<EamMouldPackageInfoDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamMouldPackageInfoDTO.class);
        page = eamMouldPackageInfoService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 模具模板框数据
     * 根据id 查询详情
     */
    @ApiOperation("查询模具模板框数据")
    @GetMapping("/get/{id}")
    public RetResult<EamMouldPackageInfoDTO> get(@PathVariable("id") String id) {
        EamMouldPackageInfoDTO item = eamMouldPackageInfoService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理模具模板框数据
     */
    @ApiOperation("保存模具模板框数据")
    @PostMapping("/save")
    @RequestLogHeader(modular = "模具模板框数据管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamMouldPackageInfoDTO eamMouldPackageInfo) {
        // 数据校验
        RetResult ret = eamMouldPackageInfoService.isValid(eamMouldPackageInfo);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamMouldPackageInfo.getId())) {
            eamMouldPackageInfo.setStatus(0);
            // 新增
            eamMouldPackageInfoService.insert(eamMouldPackageInfo);
        } else {
            // 更新
            eamMouldPackageInfoService.update(eamMouldPackageInfo);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除模具模板框数据
     */
    @ApiOperation("批量删除模具模板框数据")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "模具模板框数据管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamMouldPackageInfoDTO eamMouldPackageInfo) {
        return RetResult.ok(eamMouldPackageInfoService.logicDeleteList(eamMouldPackageInfo));
    }

    /**
     * 模具模板框数据
     */
    @ApiOperation("模具模板框数据")
    @PostMapping("/getList")
    public RetResult<?> getList(@RequestBody EamMouldPackageInfoQuery query) {

        return RetResult.ok(eamMouldPackageInfoService.getList(query));
    }

    /**
     * 模具模板框数据
     */
    @ApiOperation("模具模板框数据")
    @PostMapping("/getListAndId")
    public RetResult<?> getListAndId(@RequestBody EamMouldPackageInfoQuery query) {
        List<EamMouldPackageInfo> eamMouldPackageInfoList = eamMouldPackageInfoService.getPoList(query);
        if (StrUtil.isNotEmpty(query.getId())) {
            EamMouldPackageInfo eamMouldPackageInfo = eamMouldPackageInfoService.getById(query.getId());
            eamMouldPackageInfoList.add(eamMouldPackageInfo);
        }
        return RetResult.ok(eamMouldPackageInfoList);
    }


    /**
     * 删除模具模板框数据
     */
    @ApiOperation("删除模具模板框数据")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "模具模板框数据管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamMouldPackageInfoService.deleteItem(id);
        return RetResult.ok();
    }
}
